﻿using System;
using System.Collections.Generic;
using System.Linq;
using GC_FFW.SoccerfameParser.Abstractions.Interfaces.Repositories;
using GC_FFW.SoccerfameParser.Abstractions.Interfaces.Services;

namespace GC_FFW.SoccerfameParser.Services
{
	public class NamesService : INamesService
	{
		#region Variables

		private readonly INamesRepository _namesRepository;

		#endregion

		#region Constructors

		public NamesService(INamesRepository namesRepository)
		{
			if (namesRepository == null)
				throw new ArgumentNullException(string.Format("namesRepository"));
			_namesRepository = namesRepository;
		}

		#endregion

		#region INamesService Members

		public IDictionary<string, string> Get()
		{
			var result = _namesRepository.Get();
			return result != null && result.Items != null && result.Items.Any()
				? result.Items.ToDictionary(k => k.InvalidValue, k => k.ValidValue)
				: new Dictionary<string, string>();
		}

		public string GetValidName(string data)
		{
			var item = _namesRepository.GetByName(data, false);
			return item == null
				? data
				: item.ValidValue;
		}

		public void Save(string validName, string invalidName)
		{
			_namesRepository.Save(validName, invalidName);
		}

		public void Save(IDictionary<string, string> data)
		{
			_namesRepository.Save(data);
		}

		#endregion
	}
}
